<?php

namespace app\apishop\controller;

//use app\api\loader\Services;
use app\apishop\validate\UserValidate;
use app\common\controller\Api;
use think\Config;
use think\Exception;

/**
 * 会员接口
 */
class User extends Api
{
    protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
    protected $noNeedRight = '*';
    protected $failException = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->modelUser = new \app\admin\model\shop\User();
        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 会员中心
     */
    public function index()
    {
        $this->success('', ['welcome' => $this->auth->nickname]);
    }

    /**
     * 会员登录
     *
     * @ApiMethod (POST)
     * @param string $account 账号
     * @param string $password 密码
     * @param string $image base64 图片
     * @param type $password 1：账号密码， 2人脸识别
     */
    public function login()
    {
        $input = input('post.');
        $this->validate($input, UserValidate::class . '.login');
        $ret = $this->auth->login($input['account'], $input['pwd']);
        if ($ret) {
            try {
                $shop_info = $this->auth->getUserinfo(2);
                $data = ['userinfo' => $shop_info];
                $this->success(__('Logged in successful'), $data);
            } catch (Exception $e) {
                $this->error($e->getMessage());
            }
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * 用户详情
     * @return void
     */
    public function info()
    {
        $data = ['userinfo' => $this->auth->getUserinfo(2)];
        $this->success('ok', $data);
    }


    /**
     * 注册会员
     *
     * @ApiMethod (POST)
     */
    public function register()
    {
        $input = input('post.');
        try {
            $this->validate($input, UserValidate::class . '.shop_register');
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
        $ret = $this->auth->register($input['mobile'], $input['pwd'], $input['mobile'] . '@qq.com', $input['mobile'], []);
        if ($ret) {
            unset($input['pwd']);
            $input['user_id']=$ret;
            $this->modelUser->insert($input);
            $this->success(__('Sign up successful'));
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * 退出登录
     * @ApiMethod (POST)
     */
    public function logout()
    {
        if (!$this->request->isPost()) {
            $this->error(__('Invalid parameters'));
        }
        $this->auth->logout();
        $this->success(__('Logout successful'));
    }

    /**
     * 修改会员个人信息
     *
     * @ApiMethod (POST)
     * @param string $avatar 头像地址
     * @param string $username 用户名
     * @param string $nickname 昵称
     * @param string $bio 个人简介
     */
    public function profile()
    {
        $user = $this->auth->getUser();
        $username = $this->request->post('username');
        $nickname = $this->request->post('nickname');
        $bio = $this->request->post('bio');
        $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
        if ($username) {
            $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
            if ($exists) {
                $this->error(__('Username already exists'));
            }
            $user->username = $username;
        }
        if ($nickname) {
            $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
            if ($exists) {
                $this->error(__('Nickname already exists'));
            }
            $user->nickname = $nickname;
        }
        $user->bio = $bio;
        $user->avatar = $avatar;
        $user->save();
        $this->success();
    }

    /**
     * 修改密码
     * @return void
     */
    public function resetpwd()
    {
        $input = input('post.');
        if ($input['newpassword'] != $input['newpassword1']) {
            $this->error('原密码与确认密码不一致');
        }
        $ret = $this->auth->changepwd($input['newpassword'], $input['password'], false);
        if ($ret) {
            $this->success(__('密码修改成功请重新登录'));
        } else {
            $this->error($this->auth->getError());
        }
    }
}
